package com.db.compare.config;

import com.db.compare.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


/**
 * WebMvcConfigurationAdapter在SpringBoot2.0已过时
 * 使用WebMvcConfigurationSupport来拓展SpringMvc的功能
 * 也可以直接实现WebMvcConfigurer
 * 既保留了所有的自动配置，又可以使用我们自己拓展的
 *
 * 如果加了@EnableWebMvc  则表明，SpringBoot对SpringMvc的自动配置失效,使用当前标注的这个注解的类的配置
 */
//@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //super.addViewControllers(registry);
        //设置默认访问login页面
        registry.addViewController("/").setViewName("login");
//        registry.addViewController("/index.html").setViewName("login");
//        registry.addViewController("/main.html").setViewName("dashboard");

    }
    //注册拦截器
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/login.html","/login","/login/**","/static/**","/css/**","/icon/**","/profile/**","/webjars/**");
    }
    //静态文件

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**","/css/**","/icon/**").addResourceLocations("classpath:/static/","classpath:/css/","classpath:/icon/");
        //webjar文件
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
        registry.addResourceHandler("/profile/**").addResourceLocations("file:" + MyConfig.getProfile());

    }
}
